<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Community_model extends CI_Model {

	function fetch_community_data($name)
	{								
		
		$data =  $this->db
						->get_where('tb_mas_community',array('strName'=>$name))
						->first_row();
						
		return $data;
						
	}
	
	function fetch_content_types()
	{								
		
		$data =  $this->db
						->get_where('tb_mas_content_types')
						->result_array();
						
		return $data;
						
	}
	
	function fetch_content_type_archive($typeName,$community,$limit=10,$offset=0,$id=0)
	{
		$type = current($this->db->get_where('tb_mas_content_types',Array('strName'=>$typeName))->result_array());
			if(isset($type['intID']))
			{
				$data = $this->db
                        ->where(Array('fk-tb_mas_content_types'=>$type['intID'],'fk-tb_mas_community'=>$community,'enumEnabled'=>'enabled','intID !='=>$id))
                        ->order_by('dteStart','desc')
                        ->limit($limit,$offset)
                        ->get('tb_mas_content')   
                        ->result_array();
				if(!isset($data[0]['intID']))
				{
					$data['error'] = 'No Content Found!';
				}
			}
			else
			{
			$data['error'] = 'Type Not Found!';
			}
		return $data;
	}
	
    function fetch_content_type_shows($typeName,$community,$limit=10,$offset=0,$id=0)
	{
		$type = current($this->db->get_where('tb_mas_content_types',Array('strName'=>$typeName))->result_array());
			if(isset($type['intID']))
			{
				$data = $this->db
                        ->where(Array('fk-tb_mas_content_types'=>$type['intID'],'fk-tb_mas_community'=>$community,'intID !='=>$id))
                        ->order_by('dteStart','desc')
                        ->limit($limit,$offset)
                        ->get('tb_mas_content')   
                        ->result_array();
				if(!isset($data[0]['intID']))
				{
					$data['error'] = 'No Content Found!';
				}
			}
			else
			{
			$data['error'] = 'Type Not Found!';
			}
		return $data;
	}
    
    function fetch_content_tags($tag,$community,$limit=10,$offset=0,$id=0)
	{
		
				$data = $this->db
                        ->where(Array('strTags regexp'=>$tag,'fk-tb_mas_community'=>$community,'enumEnabled'=>'enabled'))
                        ->order_by('dteStart','desc')
                        ->limit($limit,$offset)
                        ->get('tb_mas_content')   
                        ->result_array();
				
        if(!isset($data[0]['intID']))
        {
					$data['error'] = 'No Content Found!';
        }
		
		return $data;
	}
    
	function fetch_content($type,$cid,$limit=12,$offset=0)
	{					
		
                    $this->db
						->limit($limit,$offset)
						->order_by('dteStart desc')
						->where(array('fk-tb_mas_content_types'=>$type,'fk-tb_mas_community'=>$cid));
                    
                    if($type!=5)
                        $this->db->where(array('enumEnabled'=>'enabled'));
                    
                $data = $this->db
                         ->get('tb_mas_content')
                         ->result_array();
						
		return $data;
	}
	
	
	
	
	function get_featured_video($type,$cid)
	{
		$data =  $this->db						
						->order_by('dteStart desc')
						->get_where('tb_mas_content',array('fk-tb_mas_content_types'=>$type,'fk-tb_mas_community'=>$cid,'enumEnabled'=>'enabled'))
						->first_row();
						
		return $data;
	}
	
	function fetch_single_content($id)
	{						
		$data =  $this->db												
						->get_where('tb_mas_content',array('intID'=>$id))
						->result_array();
						
		return current($data);
	}
	
	function fetch_featured_content($cid)
	{
		$data = array();
		$temp =  $this->db
						->order_by('intFeatured','desc')						
						->where(array('fk-tb_mas_community'=>$cid,'intFeatured >'=>0,'fk-tb_mas_content_types !='=>5))
						->get('tb_mas_content')
						->result_array();						
		
		foreach($temp as $t)
		{
			$item = $this->fetch_single_content($t['intID']);
			$t['contentType'] = $item['fk-tb_mas_content_types'];
			//$t['contentTitle'] = $item['strTitle'];
			//$t['contentTag'] = $item['strTags'];
			$data[] =  $t;
		}
		
		return $data;
		
	
	}
	
	function fetch_tags($tags, $id)
	{
        $data = array();    
        if($tags!=''){
            $tag_array = explode(",", $tags);
            $in_data = array();
            foreach($tag_array as $tag) {
            $temp =  $this->db
                            ->where(array('strTags regexp'=>$tag))
                            ->get('tb_mas_content')
                            ->result_array();
                foreach($temp as $t)
                {
                    if(!in_array($t['intID'],$in_data) && $t['intID'] != $id)
                    {
                        $in_data[] = $t['intID'];
                        $data[] = $t;
                    }
                }
            }
        }
		return $data;
	}
	
	private function fetch_meta($data)
	{
		
				
	
	}

}